<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>云打印系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/icons/bootstrap-icons.css">
    <link rel="stylesheet" href="/static/css/style.css">
    <style>
        :root {
            --primary-color: #0e88eb;
            --secondary-color: #57a0ff;
            --success-color: #52c41a;
            --warning-color: #faad14;
            --error-color: #f5222d;
            --text-primary: #262626;
            --text-secondary: #595959;
            --text-hint: #8c8c8c;
            --background-light: #f0f2f5;
            --border-color: #e8e8e8;
            --shadow-color: rgba(0, 0, 0, 0.1);
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--background-light);
            color: var(--text-primary);
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
        .container {
            max-width: 900px;
            margin: 15px auto;
            padding: 0 15px;
        }
        .header {
            padding: 15px 0;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            margin-bottom: 0;
            border-radius: 0;
        }
        .header h1 {
            color: white;
            font-weight: 500;
            margin-bottom: 5px;
        }
        .header p {
            color: rgba(255, 255, 255, 0.9);
            opacity: 0.9;
        }
        .content-wrapper {
            padding: 15px;
        }
        .card {
            border: none;
            box-shadow: 0 4px 12px var(--shadow-color);
            border-radius: 12px;
            overflow: hidden;
            margin-bottom: 20px;
            transition: all 0.3s;
        }
        .card:hover {
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
        }
        .card-header {
            background-color: white;
            border-bottom: 1px solid var(--border-color);
            font-weight: 500;
        }
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        .btn-primary:hover {
            background-color: var(--secondary-color);
            border-color: var(--secondary-color);
        }
        .upload-container {
            background-color: white;
            border-radius: 10px;
            padding: 20px 15px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .upload-area {
            border: 2px dashed var(--secondary-color);
            background-color: rgba(87, 160, 255, 0.05);
            border-radius: 12px;
            padding: 25px 15px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 20px;
        }
        .upload-area:hover, .upload-area.dragover {
            background-color: rgba(87, 160, 255, 0.1);
        }
        .upload-area i {
            color: var(--primary-color);
            font-size: calc(24px + 1vw);
            margin-bottom: 10px;
        }
        .upload-area h4 {
            font-size: calc(1rem + 0.5vw);
            margin-bottom: 10px;
        }
        .btn-upload {
            background-color: var(--primary-color);
            color: white;
            border-radius: 50px;
            padding: 8px 20px;
            border: none;
            box-shadow: 0 4px 6px rgba(14, 136, 235, 0.3);
            transition: all 0.3s;
        }
        .btn-upload:hover {
            background-color: var(--secondary-color);
            box-shadow: 0 6px 8px rgba(14, 136, 235, 0.4);
            transform: translateY(-2px);
        }
        #fileList {
            margin-top: 20px;
        }
        .file-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px;
            border-bottom: 1px solid #eee;
            flex-wrap: wrap;
            border-radius: 8px;
            margin-bottom: 10px;
            padding: 15px;
            background-color: white;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
            transition: all 0.2s;
        }
        .file-item:last-child {
            border-bottom: none;
        }
        .file-item:hover {
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        .file-info {
            display: flex;
            align-items: center;
            margin-bottom: 5px;
            width: 100%;
        }
        @media (min-width: 576px) {
            .file-info {
                width: auto;
                margin-bottom: 0;
            }
        }
        .file-icon {
            margin-right: 10px;
            color: #7f8c8d;
        }
        .file-name {
            font-weight: 500;
            word-break: break-all;
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        @media (min-width: 576px) {
            .file-name {
                max-width: 300px;
            }
        }
        .file-size {
            color: #7f8c8d;
            font-size: 0.85em;
            margin-left: 10px;
        }
        .status-indicator {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            width: 100%;
            margin-top: 5px;
        }
        @media (min-width: 576px) {
            .status-indicator {
                width: auto;
                margin-top: 0;
                flex-wrap: nowrap;
            }
        }
        .status-icon {
            margin-right: 5px;
        }
        .status-text {
            font-size: 0.85em;
            margin-right: 10px;
        }
        .queue-container {
            background-color: white;
            border-radius: 10px;
            padding: 15px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .queue-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            flex-wrap: wrap;
        }
        .queue-title {
            font-size: 1.2em;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 10px;
            width: 100%;
        }
        @media (min-width: 576px) {
            .queue-title {
                width: auto;
                margin-bottom: 0;
            }
        }
        .btn-refresh, .btn-sm {
            background-color: #f5f7fa;
            color: #7f8c8d;
            border: 1px solid #ddd;
            font-size: 0.85em;
            padding: 8px 12px;
        }
        .btn-refresh:hover, .btn-sm:hover {
            background-color: #ecf0f1;
            color: #2c3e50;
        }
        .queue-stats {
            display: flex;
            margin-bottom: 15px;
            flex-wrap: wrap;
        }
        .stat-item {
            background-color: #f5f7fa;
            padding: 10px 15px;
            border-radius: 5px;
            margin-right: 10px;
            margin-bottom: 10px;
            width: 100%;
        }
        @media (min-width: 576px) {
            .stat-item {
                width: auto;
                flex: 1;
                margin-bottom: 0;
            }
        }
        .stat-label {
            font-size: 0.8em;
            color: #7f8c8d;
        }
        .stat-value {
            font-size: 1.5em;
            font-weight: 600;
            color: #2c3e50;
        }
        .status-success {
            color: var(--success-color);
        }
        .status-error {
            color: var(--error-color);
        }
        .status-warning {
            color: var(--warning-color);
        }
        .status-pending {
            color: var(--primary-color);
        }
        .loading {
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 3px solid rgba(0, 0, 0, 0.1);
            border-radius: 50%;
            border-top-color: var(--primary-color);
            animation: spin 1s ease-in-out infinite;
        }
        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }
        .alert {
            margin-top: 20px;
        }
        .hidden {
            display: none;
        }
        #supportedFormats {
            color: #7f8c8d;
            font-size: 0.9em;
            margin-top: 5px;
        }
        .card {
            overflow: hidden;
        }
        .card-header {
            padding: 12px 15px;
        }
        .card-body {
            padding: 15px;
        }
        .input-group {
            margin-bottom: 10px;
        }
        .form-select {
            padding: 0.375rem 0.75rem;
            font-size: 0.95rem;
        }
        @media (max-width: 576px) {
            .row {
                margin: 0 -8px;
            }
            .col-md-6 {
                padding: 0 8px;
            }
            h5, h4, h3 {
                font-size: 1.1rem;
            }
            .btn {
                padding: 0.375rem 0.75rem;
            }
        }
        /* 底部导航栏优化 */
        .mobile-nav {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: white;
            box-shadow: 0 -1px 8px rgba(0, 0, 0, 0.08);
            padding: 8px 0;
            z-index: 1000;
            height: 68px;
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            background-color: rgba(255, 255, 255, 0.95);
            border-top: 1px solid rgba(0, 0, 0, 0.05);
        }
        
        .mobile-nav .nav-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: #8c8c8c;
            text-decoration: none;
            font-size: 0.75rem;
            transition: all 0.25s ease;
            padding: 6px 0;
            position: relative;
            height: 52px;
        }
        
        .mobile-nav .nav-item i {
            font-size: 1.5rem;
            margin-bottom: 2px;
            transition: all 0.25s ease;
        }
        
        .mobile-nav .nav-item.active {
            color: var(--primary-color);
            font-weight: 500;
        }
        
        .mobile-nav .nav-item.active i {
            transform: translateY(-2px);
        }
        
        .mobile-nav .nav-item.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 4px;
            height: 4px;
            border-radius: 50%;
            background-color: var(--primary-color);
            transition: all 0.25s ease;
        }
        
        /* 图标动画效果 */
        .mobile-nav .nav-item i {
            position: relative;
            z-index: 1;
        }
        
        .mobile-nav .nav-item::before {
            content: '';
            position: absolute;
            top: 10px;
            left: 50%;
            transform: translateX(-50%) scale(0);
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: rgba(14, 136, 235, 0.08);
            z-index: 0;
            transition: transform 0.25s ease;
        }
        
        .mobile-nav .nav-item.active::before {
            transform: translateX(-50%) scale(1);
        }
        
        /* 导航条触摸反馈 */
        @media (hover: hover) {
            .mobile-nav .nav-item:hover {
                color: var(--primary-color);
            }
            
            .mobile-nav .nav-item:hover::before {
                transform: translateX(-50%) scale(0.7);
            }
        }
        
        .mobile-nav .nav-item:active {
            transform: scale(0.95);
        }
        /* 为底部导航腾出空间 */
        @media (max-width: 767px) {
            body {
                padding-bottom: 70px;
            }
            .content-wrapper {
                padding: 10px;
            }
            .card {
                margin-bottom: 15px;
            }
        }
        /* 页面内容区域样式 */
        .content-section {
            display: none;
            animation: fadeInUp 0.3s ease-out;
        }
        .content-section.active {
            display: block;
        }
        /* 动画效果 */
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .btn {
            transition: all 0.3s;
        }
        .btn-outline-primary {
            color: var(--primary-color);
            border-color: var(--primary-color);
        }
        .btn-outline-primary:hover {
            background-color: var(--primary-color);
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="container">
            <center>
            <h1>云打印系统</h1>
            <p class="mb-0">简单、快速的文档打印服务</p>
           </center>
        </div>
    </div>

    <div class="content-wrapper">
        <div class="container">
            <div class="card upload-container content-section active" id="upload-section">
                <div class="card-body">
                    <h3 class="d-flex align-items-center mb-3">
                        <i class="bi bi-cloud-upload me-2"></i>文件上传
                    </h3>
                    <div id="uploadArea" class="upload-area">
                        <i class="bi bi-cloud-upload"></i>
                        <h4 class="mb-2">拖放文件到这里或</h4>
                        <p class="text-muted d-none d-sm-block">快速上传多种文档格式</p>
                        <p id="supportedFormats" class="text-muted mb-3 small">支持: PDF, DOC, DOCX, XLS, JPG等常见格式</p>
                        <input type="file" id="fileInput" class="hidden" multiple>
                        <button id="selectFileBtn" class="btn btn-upload">
                            <span class="btn-icon-wrapper">
                                <i class="bi bi-files"></i>
                            </span>
                            <span class="btn-text">选择文件</span>
                            <span class="d-none d-sm-inline btn-sub-text">（支持多文件）</span>
                        </button>
                    </div>
                    
                    <div id="alerts" class="mt-3"></div>
                    
                    <div id="fileList" class="mt-3"></div>
                </div>
            </div>
            
            <div class="card content-section" id="printer-section">
                <div class="card-header d-flex justify-content-between align-items-center flex-wrap">
                    <span class="mb-2 mb-sm-0"><i class="bi bi-printer-fill me-2"></i>打印机设置</span>
                    <button class="btn btn-sm btn-primary" onclick="refreshStatus()">
                        <i class="bi bi-arrow-clockwise"></i> 刷新
                    </button>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-12 col-sm-6 mb-3">
                            <div class="mb-3">
                                <label for="printer-select" class="form-label fw-bold">选择打印机:</label>
                                <div class="input-group mt-1">
                                    <select class="form-select" id="printer-select">
                                        <option value="">加载中...</option>
                                    </select>
                                    <button class="btn btn-primary" type="button" id="btn-set-printer">设置</button>
                                </div>
                            </div>
                            <div class="mb-3">
                                <button class="btn btn-outline-primary" id="btn-test-printer">
                                    <i class="bi bi-printer"></i> 打印测试页
                                </button>
                            </div>
                        </div>
                        <div class="col-12 col-sm-6">
                            <div class="mb-4">
                                <div class="bg-light p-3 rounded d-flex align-items-center">
                                    <i class="bi bi-hdd me-3 fs-4 text-primary"></i>
                                    <div>
                                        <div class="small text-muted">打印机状态</div>
                                        <div class="fw-bold" id="printer-status">正常</div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="mb-2">
                                        <strong>已处理:</strong> <span id="processedCount">-</span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-2">
                                        <strong>已打印:</strong> <span id="printedCount">-</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card queue-container content-section" id="queue-section">
                <div class="card-header d-flex justify-content-between align-items-center mb-3 flex-wrap">
                    <span class="mb-2 mb-sm-0"><i class="bi bi-list-ul me-2"></i>打印队列</span>
                    <button id="refreshBtn" class="btn btn-sm btn-primary">
                        <i class="bi bi-arrow-clockwise"></i> 刷新
                    </button>
                </div>
                <div class="card-body">
                    <div class="row mb-4">
                        <div class="col-6 col-md-4 mb-3 mb-md-0">
                            <div class="bg-light p-3 rounded">
                                <div class="small text-muted">已处理文件</div>
                                <div class="fs-3 fw-bold" id="processedCount2">0</div>
                            </div>
                        </div>
                        <div class="col-6 col-md-4 mb-3 mb-md-0">
                            <div class="bg-light p-3 rounded">
                                <div class="small text-muted">已打印文件</div>
                                <div class="fs-3 fw-bold" id="printedCount2">0</div>
                            </div>
                        </div>
                        <div class="col-12 col-md-4">
                            <div class="bg-light p-3 rounded">
                                <div class="small text-muted">队列中文件</div>
                                <div class="fs-3 fw-bold" id="queueCount">0</div>
                            </div>
                        </div>
                    </div>
                    
                    <h5 class="d-flex align-items-center mb-3">
                        <i class="bi bi-hourglass me-2 text-warning"></i>
                        <span>等待打印的文件</span>
                        <span class="badge bg-warning text-dark ms-2 fs-6" id="waitingCount">0</span>
                    </h5>
                    <div id="queueList" class="mb-4">
                        <div class="text-center text-muted py-3">加载中...</div>
                    </div>
                    
                    <h5 class="d-flex align-items-center mb-3">
                        <i class="bi bi-clock-history me-2 text-success"></i>
                        <span>最近打印的文件</span>
                        <span class="badge bg-success text-white ms-2 fs-6" id="recentCount">0</span>
                    </h5>
                    <div id="recentPrintsList">
                        <div class="text-center text-muted py-3">加载中...</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 底部导航栏 -->
    <nav class="mobile-nav">
        <a href="#upload-section" class="mobile-nav-item" data-section="upload-section">
            <i class="bi bi-upload"></i>
            <span>上传</span>
        </a>
        <a href="#printer-section" class="mobile-nav-item" data-section="printer-section">
            <i class="bi bi-printer-fill"></i>
            <span>打印</span>
        </a>
        <a href="#queue-section" class="mobile-nav-item" data-section="queue-section">
            <i class="bi bi-card-list"></i>
            <span>队列</span>
        </a>
    </nav>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/main.js"></script>
    <script>
    // 加载打印机列表
    function loadPrinters() {
        fetch('/api/printers')
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    const select = document.getElementById('printer-select');
                    select.innerHTML = '';
                    
                    // 更新打印机状态
                    const printerStatusEl = document.getElementById('printer-status');
                    if (printerStatusEl) {
                        printerStatusEl.textContent = data.printers.length > 0 ? '正常' : '未找到打印机';
                        printerStatusEl.className = data.printers.length > 0 ? 'fw-bold text-success' : 'fw-bold text-danger';
                    }
                    
                    data.printers.forEach(printer => {
                        const option = document.createElement('option');
                        option.value = printer.name;
                        option.text = printer.name + (printer.is_default ? ' (系统默认)' : '');
                        if (printer.is_selected) {
                            option.selected = true;
                        }
                        select.appendChild(option);
                    });
                }
            })
            .catch(error => {
                console.error('获取打印机列表失败:', error);
                const printerStatusEl = document.getElementById('printer-status');
                if (printerStatusEl) {
                    printerStatusEl.textContent = '连接失败';
                    printerStatusEl.className = 'fw-bold text-danger';
                }
            });
    }

    // 设置默认打印机
    document.getElementById('btn-set-printer').addEventListener('click', function() {
        const select = document.getElementById('printer-select');
        const printer = select.value;
        
        if (!printer) {
            showAlert('请选择打印机', 'warning');
            return;
        }
        
        // 显示加载状态
        this.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 设置中...';
        this.disabled = true;
        
        fetch('/api/set-default-printer', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                printer_name: printer
            })
        })
        .then(response => response.json())
        .then(data => {
            this.innerHTML = '设置';
            this.disabled = false;
            
            if (data.status === 'success') {
                showAlert('默认打印机已设置', 'success');
            } else {
                showAlert('设置打印机失败: ' + data.detail, 'danger');
            }
        })
        .catch(error => {
            console.error('设置打印机失败:', error);
            showAlert('设置打印机失败', 'danger');
            this.innerHTML = '设置';
            this.disabled = false;
        });
    });

    // 打印测试页
    document.getElementById('btn-test-printer').addEventListener('click', function() {
        // 显示加载状态
        this.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 打印中...';
        this.disabled = true;
        
        fetch('/api/test-printer')
            .then(response => response.json())
            .then(data => {
                this.innerHTML = '<i class="bi bi-printer"></i> 打印测试页';
                this.disabled = false;
                
                if (data.status === 'success') {
                    showAlert('测试页已发送到打印机', 'success');
                } else {
                    showAlert('打印测试页失败: ' + data.message, 'danger');
                }
            })
            .catch(error => {
                console.error('打印测试页失败:', error);
                showAlert('打印测试页失败', 'danger');
                this.innerHTML = '<i class="bi bi-printer"></i> 打印测试页';
                this.disabled = false;
            });
    });

    // 初始化时加载打印机列表
    document.addEventListener('DOMContentLoaded', function() {
        loadPrinters();
        // 其他初始化代码...
        refreshStatus();
        
        // 初始化底部导航
        initNavigation();
    });
    
    // 初始化导航栏
    function initNavigation() {
        const navItems = document.querySelectorAll('.mobile-nav-item');
        const sections = document.querySelectorAll('.content-section');
        
        // 设置默认激活的导航项
        const firstNavItem = document.querySelector('.mobile-nav-item');
        if (firstNavItem) {
            firstNavItem.classList.add('active');
            const firstSectionId = firstNavItem.getAttribute('data-section');
            const firstSection = document.getElementById(firstSectionId);
            if (firstSection) {
                sections.forEach(s => s.classList.remove('active'));
                firstSection.classList.add('active');
            }
        }
        
        // 为导航项添加点击事件
        navItems.forEach(item => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                
                // 移除所有激活状态
                navItems.forEach(i => i.classList.remove('active'));
                
                // 添加当前激活状态
                item.classList.add('active');
                
                // 显示对应的部分
                const sectionId = item.getAttribute('data-section');
                sections.forEach(s => s.classList.remove('active'));
                document.getElementById(sectionId).classList.add('active');
                
                // 添加轻微振动反馈（如果设备支持）
                if (navigator.vibrate) {
                    navigator.vibrate(10);
                }
                
                // 滚动到顶部
                window.scrollTo(0, 0);
                
                console.log('导航切换到:', sectionId);
            });
        });
    }
    
    // 活动状态同步
    function refreshStatus() {
        loadQueueStatus();
        loadPrinters();
    }
    
    // 更新队列统计数据的扩展
    const originalUpdateQueueStats = updateQueueStats;
    updateQueueStats = function(data) {
        originalUpdateQueueStats(data);
        
        // 更新第二组计数器
        const processedCount2 = document.getElementById('processedCount2');
        const printedCount2 = document.getElementById('printedCount2');
        
        if (processedCount2) {
            processedCount2.textContent = data.processed_count || 0;
        }
        
        if (printedCount2) {
            printedCount2.textContent = data.printed_count || 0;
        }
    };
    </script>
</body>
</html> 